import axios from 'axios'
import NProgress from 'nprogress'
import {
    message
} from 'antd';
const instance = axios.create({
    baseURL: 'http://localhost:3000/api',
    timeout: 1000
});
instance.interceptors.request.use(function (config) {
    NProgress.start()
    if (sessionStorage.getItem("token")) {
        config.headers.Authorization = sessionStorage.getItem("token")
    }
    // Do something before request is sent
    return config;
}, function () {
    // Do something with request error
    message.error("服务器后台未响应")
    return;
});

// Add a response interceptor
instance.interceptors.response.use(function (response) {
    // Do something with response data
    const {
        data: res
    } = response
    if (res.code !== 10000) {
        message.error(res.msg)
        if (res.code === 50003) {
            window.sessionStorage.clear()
            window.location.href="/login"
        }
        NProgress.done()
        return
    }
    NProgress.done()
    return response;
}, function (e) {
    // Do something with response error
    NProgress.done()
    message.error("请求后台失败")
});

export default instance;